package de.uni.kassel.campusappver3.crawler;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.AsyncTask;

import de.uni.kassel.campusappver3.MainActivity;
import de.uni.kassel.campusappver3.model.CrawlerReturnValue;
/**
 * This is the abstract Crawler class.
 * 
 * The Crawlers are using strong and weak regex to get the data out of the HTML of the targetsite
 * 
 * get all values:
  for (String key : crv.getKeys()) {
			System.out.println("key: " + key);
			for (String string : crv.getValue(key)) {
				System.out.println(string);				
			}
			
		}
 * @author ckonze
 *
 */
public abstract class Crawler extends AsyncTask<Object , Object, CrawlerReturnValue> {
	protected ICrawl listener;
	
	public Crawler(ICrawl listener){
		this.listener = listener;
	}	
	
	/**
	 * starts a crwal
	 */
	public void fetch(){
		Object params = null;
		this.execute(params);

	}
	/**
	 * starts a crwal with the given params
	 */
	public void fetch(Object[] params){
		this.execute(params);
	}
	
	protected CrawlerReturnValue doInBackground(Object... params){
		CrawlerReturnValue crv = new CrawlerReturnValue();
		return this.backgoundCrawl(params, crv);		
	}
	
	
	protected abstract CrawlerReturnValue backgoundCrawl(Object[] params, CrawlerReturnValue crv);

	/**
	 * calls the callback method of the listener and returns the values
	 */
	protected void onPostExecute(CrawlerReturnValue result) {
		listener.onCrawlFinish(result);
	}	
	
	/**
	 * fetches the html of the given URL and returns a matchter corresponding to
	 * the given regex
	 * 
	 * @param URL
	 * @param RegEx
	 * @return
	 */
	protected Matcher getUrlMatcher(String URL, String RegEx) {
		String html = this.getHtml(URL);
		Pattern pat = Pattern.compile(RegEx);
		return pat.matcher(html);
	}
	
	/**
	 * returns a matchter corresponding to the given regex on the given string
	 * 
	 * @param RegEx
	 * @return
	 */
	protected Matcher getStringMatcher(String string, String RegEx) {
		Pattern pat = Pattern.compile(RegEx);
		return pat.matcher(string);
	}
	
	
	
	/***
	 * return the html of the given urlstring
	 * 
	 * @param urlString
	 * @return
	 */
	private String getHtml(String urlString) {
		StringBuilder response = new StringBuilder();

		try {
			URL website = new URL(urlString);

			URLConnection connection = website.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "iso-8859-1"));

			String inputLine;

			while ((inputLine = in.readLine()) != null)
				response.append(inputLine);

			in.close();
		} catch (Exception e) {
			System.out.println("Error: Crawler.getHtml():");
			e.printStackTrace();
		}
		return response.toString();
	}

	/**
	 * string.replaceAll("<(.*?)>", "").replaceAll("&(.*?);", "");
	 * @param string
	 * @return
	 */
	protected String removeTagsEntitys(String string){
		string = string.replaceAll("<(.*?)>", " ")
					   .replaceAll("&#252;", ""+(char) 252)				
					   .replaceAll("&#223;", ""+(char) 223)
					   .replaceAll("&#228;", ""+(char) 228)
					   .replaceAll("&#246;", ""+(char) 246);		
		return string.replaceAll("&(.*?);", " ");
	}
}
